<!-- 问题反馈 -->
<template>
	<view class="pb140">
	<uni-forms ref="formref" :modelValue="formdata" :rules="formrules" labelWidth="190px" label-position="top" class="mb10">
		<uni-section title="基本信息" type="line" style="background: #efeff4;" />
		<uni-card :is-shadow="false" padding="20px 10px 10px" spacing="0" margin="5px 0 5px 0">
			<uni-forms-item label="用户户号" name="yhbh">
				<uni-easyinput v-model="formdata.yhbh" disabled />
			</uni-forms-item>
			<uni-forms-item label="用户名称" name="yhmc">
				<uni-easyinput v-model="formdata.yhmc"/>
			</uni-forms-item>
			<uni-forms-item label="联系方式" name="lxsj">
				<uni-easyinput v-model="formdata.yhdh" placeholder="联系方式" disabled />
			</uni-forms-item>
			<uni-forms-item label="用电地址" name="yhdz">
				<uni-easyinput v-model="formdata.yhdz"  disabled/>
			</uni-forms-item>
		</uni-card>
		<uni-section :title="headertitle" type="line" style="background: #efeff4;" />

		<uni-card :is-shadow="false" padding="20px 10px 10px" spacing="0" margin="5px 0 5px 0">
			<uni-forms-item :label="headertitle+ '内容'" name="bxnr" required>
				<uni-easyinput type="textarea" v-model="formdata.bxnr" :placeholder="headertitle+ '内容'" />
			</uni-forms-item>
			<uni-forms-item label="材料" name="bxnr">
				<uni-file-picker limit="9" title="最多选择9张图片"></uni-file-picker>
			</uni-forms-item>
		</uni-card>
	</uni-forms>
	</view>
	
	<view class="button-submit"><button type="primary" @click="formFubmit">提交</button></view>

	<cUserinfoSelect v-if="!formdata.yhbh" @setYhbh="doSelectYhbh" />
</template>

<script setup lang="ts">
	import cUserinfoSelect from "@/pages/bspsd/components/userinfo-select.vue";
	import { GetYhJbxx } from "@/api/request/GetYhJbxx";
	import { gitDataTime } from "@/utils/index";
	import { getJgbm } from "@/utils/storage";
	import { GzdGzslAdd } from "@/api/request/GzdGzslAdd";
	import { ref } from 'vue';
	import { onLoad } from '@dcloudio/uni-app'

	let formref = ref(null)
	let formdata = ref({
		yhbh: '',
		yhmc: '',
		yhdh: '',
		yhdz: '',
		sqsj: gitDataTime(),
		sqlx: '',
		bxnr: '',
		gzdzt: '',
		lxsj: ''
	})
	let formrules = {
		sqsj: { rules: [{ required: true, errorMessage: '请选择故诉求时间' }] },
		sqlx: { rules: [{ required: true, errorMessage: '请选择诉求类型' }] },
		bxnr: { rules: [{ required: true, errorMessage: '请填写诉求内容' }] },
	}
	let headertitle = ref('')
	onLoad((option) => {
		headertitle.value = option.dmnr || '问题咨询'
		formdata.value.sqlx = option.dmbh + '-' + option.dmnr
		formdata.value.gzdzt = option.dmbh
		uni.setNavigationBarTitle({
			title: headertitle.value,
		});
	});
	
	function formFubmit() {
		formref.value.validate().then( () => {
			GzdGzslAdd({
				data: {
					jgbm: getJgbm(),
					bxslr: "微信公众号",
					bxslrid: "9999",
					gzdh: Date.now(),
					...formdata.value
				},
				success: (res) => {
					uni.showModal({
						title: "提示",
						content: "你的已提交成功，稍后可以在：我的->问题反馈中查询！",
						showCancel: false,
						success: () => {
							uni.navigateBack()
						},
					})
				},
				fail: (code, message) => {
					console.log(code, message)
				}
			})
		}).catch(err => {
			// console.log('表单错误信息：', err);
			uni.showToast({ icon: "none", title: err[0].errorMessage })
		})
	}
	function doSelectYhbh(yhbh : string) {
		GetYhJbxx({
			data: { yhbh: yhbh },
			success: (res) => {
				if (res.length != 1) {
					uni.showModal({
						title: '错误提示',
						content: '查询数据时发生内部逻辑错误',
						showCancel: false,
						success: function () {
							uni.navigateBack()
						}
					})
					return
				}
				formdata.value = Object.assign({}, formdata.value, res[0])
				formdata.value.yhdh = res[0].lxsj
			},
			fail: (_code, message) => {
				uni.showModal({ content: message, showCancel: false })
				uni.navigateBack()
			}
		})
	}
</script>

<style lang="scss" scoped>
	.trading-repair-dom {
		background: #fff;
	}
</style>